package com.example.myapplication.tack_30.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_30.data.AddressData;
import com.example.myapplication.tack_30.data.ProductData;
import com.example.myapplication.utils.OkHttpTool;
import com.google.gson.Gson;

import java.util.List;

public class CaiDanAdapter extends RecyclerView.Adapter<CaiDanAdapter.My> {
    private List<ProductData.DataDTO> list;
    private Context context;
    private View view;
    private Gson gson;
    public CaiDanAdapter(List<ProductData.DataDTO> list, Context context) {
        this.list = list;
        this.context = context;
        gson = new Gson();
    }

    @NonNull
    @Override
    public My onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.tack_32_cai_dan,null);
        return new My(view);
    }

    @Override
    public void onBindViewHolder(@NonNull My holder, int position) {

        Glide.with(context).load(OkHttpTool.URL+list.get(position).getImgUrl()).into(holder.img);
        holder.name.setText(list.get(position).getName());
        int number = list.get(position).getNumber();
        float price = (float) list.get(position).getPrice();
        holder.amount.setText("￥ "+String.valueOf(price*number));
        holder.number.setText("*"+number);


    }
    @Override
    public int getItemCount() {

        return list != null?list.size():0;
    }

    public class My extends RecyclerView.ViewHolder{
        private ImageView img;
        private TextView name;
        private TextView amount;
        private TextView number;
        public My(@NonNull View itemView) {
            super(itemView);
            name = view.findViewById(R.id.name);
            img = view.findViewById(R.id.img);
            amount = view.findViewById(R.id.amount);
            number = view.findViewById(R.id.number);
        }
    }
}
